package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.services.infrastructure.bean.MessageCenterBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.MessageCenterMapper;
import com.molichuxing.services.infrastructure.dto.request.create.MessageCenterCreateDto;
import com.molichuxing.services.infrastructure.dto.response.MessageCenterDto;
import com.molichuxing.services.infrastructure.service.MessageCenterService;
import com.molichuxing.services.property.MessageCenterStatusEnum;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: renzhengyu
 * @Description:
 * @Date: Created in 10:36 2019/10/10
 * @Modified By:
 **/
@Service("messageCenterService")
public class MessageCenterServiceImpl implements MessageCenterService {
    private static final Logger log = Logger.getLogger(MessageCenterServiceImpl.class);

    @Autowired
    private MessageCenterMapper messageCenterMapper;

    @Override
    public int createMessageCenter(MessageCenterCreateDto messageCenterCreateDto) {
        MessageCenterBean messageCenterBean = Convert.toMessageCenterBean(messageCenterCreateDto);
        messageCenterMapper.create(messageCenterBean);
        return messageCenterBean.getId();
    }

    @Override
    public Paged<MessageCenterDto> getMessageCenterPage(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        int count = messageCenterMapper.getMessageCenterPageCount(map);
        if(count <= 0) {
            return new Paged<>(null,count,pageSize);
        }
        Paged<MessageCenterDto> paged = new Paged<>();
        List<MessageCenterBean> result = messageCenterMapper.getMessageCenterPage(map, pageNum, pageSize);
        log.info("根据条件查询消息列表 出参：result： "+ JSON.toJSONString(result));
        paged.setList(Convert.toMessageCenterDto(result));
        paged.setTotal(count);
        paged.setPageSize(pageSize);
        return paged;
    }

    @Override
    public Integer getUnreadCount(Integer userId) {
        return messageCenterMapper.getUnreadCount(userId);
    }

    /**
     * 将用户所有消息改为已读
     * @param userId
     * @return
     */
    @Override
    public int modifyStatusByUserId(Integer userId) {
        return messageCenterMapper.modifyStatusByUserId(userId, MessageCenterStatusEnum.READ.getValue());
    }
}
